package com.example.Blog.service;

import com.example.Blog.dao.UserDao;
import com.example.Blog.model.User;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
@Transactional
public class UserService {

    final UserDao userDao;

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    public User login(User user) {
        User destinationUser = this.userDao.findByUserAccount(user.getUserAccount());
        if (destinationUser != null && destinationUser.getUserPassword().equals(user.getUserPassword()))
            return destinationUser;
        else return null;
    }

    /**
     * 注册
     *
     * @param user 传入一个对象
     * @return
     */
    public User register(User user) {
        this.userDao.save(user);
        return this.userDao.findByUserAccount(user.getUserAccount());
    }

    /**
     * 从数据库中删除用户，即注销
     *
     * @param userId 用户id
     */
    public void delete(int userId) {
        userDao.delete(userDao.findByUserId(userId));
    }

    // TODO: 2023/11/20
    //  退出登录
    void signOut() {

    }

    // TODO: 2023/11/19
    //  使用异步处理，采用消息队列或者异步任务处理库在后台更新粉丝数
    public void follow(int uid) {
        User destinationUser = userDao.findByUserId(uid);
        userDao.updateByUserId(destinationUser.getUserFans() + 1, destinationUser.getUserId());
    }

    // TODO: 2023/11/20
    //  黑名单功能，拉黑用户
    void block() {

    }

    public User findUserByNickname(String userNickname) {
        return this.userDao.findByUserNickname(userNickname);
    }

    public User findUser(int userId) {
        return this.userDao.findByUserId(userId);
    }

    public User findUserByAccount(String userAccount) {
        return this.userDao.findByUserAccount(userAccount);
    }

    public void savePhoto(User user, MultipartFile avatar) {
        String uploadDirectory = "target/classes/static/photo/"; // 指定保存文件的目录路径

        // 创建目标目录（如果不存在）
        File directory = new File(uploadDirectory);
        if (!directory.exists()) {
            directory.mkdirs();
        }

        String fileName = user.getUserAccount() + ".png";

        try {
            // 创建目标文件
            File file = new File(uploadDirectory + fileName);

            // 将上传的文件复制到目标文件
            FileCopyUtils.copy(avatar.getBytes(), file);

            // 文件保存成功
            System.out.println("文件保存成功：" + file.getAbsolutePath());
        } catch (IOException e) {
            // 处理保存文件时的异常
            e.printStackTrace();
        }
    }

    public boolean verifyValidAccount(String userAccount) {
        return this.userDao.findByUserAccount(userAccount) != null;
    }

    public void updateUserPassword(String newUserPassword, int userId){
        this.userDao.updateUserByUserId(newUserPassword, userId);
    }

    public void updateUserNickname(String newUserNickname, int userId){
        this.userDao.updateByUserId(newUserNickname, userId);
    }
}
